﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace HospitalSystem
{
    public partial class BeforeRecordData : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int analysistime;
        int comparetoBreakLoop = 1;
        protected void showsession()
        {
            String ShowValue = (string)Session["CID"];
            Label3.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            Label5.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            Label4.Text = ShowValue3;


            //String ShowValue4 = (string)Session["CID"];
            //SelectStat.Text = ShowValue4;
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            showsession();
            if (!IsPostBack)
            {
                ResetValue();
                String CID = (string)Session["CID"];
                var query = from a in db.Evaluations
                            join b in db.Persons on a.CID equals b.CID
                            where a.CID.Equals(CID)
                            select new
                            {
                                a.CaseNo,
                                b.FirstName,
                                b.LastName,
                                //b.DOB,
                                a.EvalDateTime
                            };
                if (query.FirstOrDefault() == null)
                {
                    MessageBox("Error!!!!!!!!!");
                    Panel2.Visible = false;
                }
                else
                {
                    DropDownList1.Items.Add(new ListItem("===เลือกการประเมิน===", "Zero"));
                    Panel2.Visible = true;
                    Label3.Text = CID;
                    foreach (var q in query)
                    {
                        analysistime++;
                        Label4.Text = q.FirstName;
                        Label5.Text = q.LastName;
                        //Label7.Text = TextBox1.Text;
                        //int age = DateTime.Now.Year - q.DateofBirth.Year;
                        //if (DateTime.Now < q.DateofBirth.AddYears(age))
                        //{
                        //    age--;
                        //}
                        //Label6.Text = age.ToString();
                        DropDownList1.Items.Add(q.CaseNo.ToString());
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Label3.Text == String.Empty || Label7.Text == String.Empty)
            {
                MessageBox("ลงข้อมูลไม่ครบ ไม่สามารถเลือกบันทึกข้อมูลได้");
            }
            else
            {
                Session["ID"] = Label3.Text;
                Session["CaseNumber"] = Label7.Text;
                Response.Write("<script>");
                Response.Write("window.open('RecordChildData.aspx')");
                Response.Write("</script>");
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel2.Visible = true;
            if (DropDownList1.SelectedValue == "Zero")
            {
                Label7.Text = "";
            }
            else
            {
                int timeChoosing = Convert.ToInt32(DropDownList1.SelectedValue);
                var query = from a in db.Evaluations
                            where Label3.Text.Equals(a.CID) && a.CaseNo.Equals(timeChoosing)
                            select a.CaseNo;
                foreach (var q in query)
                {
                    Label7.Text = q.ToString();
                }
            }
        }
        private void ResetValue()
        {
            Label3.Text = "";
            Label4.Text = "";
            Label5.Text = "";
            Label6.Text = "";
            Label7.Text = "";
            DropDownList1.Items.Clear();
        }
    }
}